package com.yanqu.road.server.manger.activity.drawPictures;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.drawPictures.PicturesComb;
import com.yanqu.road.entity.activity.drawPictures.PicturesWeight;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.drawPictures.DrawPicturesActivityModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DrawPicturesActivityMgr extends TempMgr {

    private static Logger logger = LogManager.getLogger(DrawPicturesActivityMgr.class.getName());

    //activityId - combId
    private static Map<Integer, Map<Integer, PicturesComb>> picturesCombConfigMap;  //年画组合主题
    //activityId - id
    private static Map<Integer, Map<Integer, PicturesWeight>> picturesWeightConfigMap;  //年画权重

    private static RandomHelper random = new RandomHelper();

    public static Map<Integer, PicturesWeight> getPictureWeightMap(int activityId) {
        return new ConcurrentHashMap<>(picturesWeightConfigMap.get(activityId));
    }

    public static Map<Integer, PicturesComb> getPicturesCombMap(int activityId) {
        return new ConcurrentHashMap<>(picturesCombConfigMap.get(activityId));
    }

    /**
     * 画笔道具id
     * @param activityInfo
     * @return
     */
    public static int getActivityConsumeId(ActivityInfo activityInfo) {
        return StringUtils.stringToIntegerList(activityInfo.getParam4(), "\\|").get(0);
    }

    public static String getPicturesDesc(ActivityInfo activityInfo) {
        return activityInfo.getParam3();
    }

    /**
     * 活动积分id
     * @param activityInfo
     * @return
     */
    public static int getActivityScoreId(ActivityInfo activityInfo) {
        return StringUtils.stringToIntegerList(activityInfo.getParam4(), "\\|").get(1);
    }

    @Override
    public boolean reloadData() throws Exception {
        picturesCombConfigMap = new ConcurrentHashMap<>();
        picturesWeightConfigMap = new ConcurrentHashMap<>();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityInfo() {
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.DrawPicturesActivity.getValue());
        if (activityInfoList != null && activityInfoList.size() > 0) {
            List<Integer> activityIdList = new ArrayList<>();
            for (ActivityInfo activityInfo : activityInfoList) {
                activityIdList.add(activityInfo.getActivityId());
            }
            Map<Integer, Map<Integer, PicturesComb>> tempCombConfigMap = ActivityBussiness.getPicturesCombConfigMap(activityIdList);
            Map<Integer, Map<Integer, PicturesWeight>> tempWeightConfigMap = ActivityBussiness.getPicturesWeightConfigMap(activityIdList);

            picturesCombConfigMap = tempCombConfigMap;
            picturesWeightConfigMap = tempWeightConfigMap;
            playerReloadActivity();
        }
        logger.info("reload DrawPictures activity end");
    }

    /**
     * 随机年画
     * @param activityId
     * @return
     */
    public static int randomPictures(int activityId) {
        int sumWeight = 0;
        Map<Integer, PicturesWeight> pictureWeightMap = getPictureWeightMap(activityId);
        List<PicturesWeight> picturesWeights = new ArrayList<>(pictureWeightMap.values());
        for (PicturesWeight pictures : picturesWeights) {
            sumWeight += pictures.getWeight();
        }
        int randomValue = random.next(sumWeight) + 1;
        int addValue = 0;
        for (PicturesWeight pictures : picturesWeights) {
            addValue += pictures.getWeight();
            if (addValue >= randomValue) {
                return pictures.getId();
            }
        }
        return 0;
    }

    private static void playerReloadActivity() {
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        for (GamePlayer onlinePlay : playerList) {
            onlinePlay.getModule(DrawPicturesActivityModule.class).syncActivity();
        }
    }
}
